<?php


namespace App\Web\Controller;


use App\Web\Model\User as UserModel;
use EchoPHP\Facades\Validation;
use EchoPHP\Http\Request;
use EchoPHP\Http\Response\JsonResponse;
use EchoPHP\Validator\Validator;
use Exception;

class Login extends BaseController {

    /**
     * 检验邮箱是否被注册
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function checkEmail(Request $request) {
        $email = $request->getGet('email');
        if (!$email) {
            return $this->json(['status' => false, 'msg' => '请输入邮箱']);
        }
        if (!Validation::validateEmail('email', $email)) {
            return $this->json(['status' => false, 'msg' => '邮箱格式不正确']);
        }
        $userModel = new UserModel();
        $userInfo = $userModel->getUserByEmail($email);
        if (!empty($userInfo)) {
            return $this->json(['status' => false, 'msg' => '该邮箱已被注册']);
        }
        return $this->json(['status' => true, 'msg' => '']);
    }

    /**
     * 用户注册
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function register(Request $request) {
        $email = $request->getPost('email');
        $password = $request->getPost('password');
        $password_confirm = $request->getPost('password_confirm');
        $nickname = $request->getPost('nickname');

        $rule = [
            'email|邮箱' => ['email', 'required', 'minLength' => 1, 'maxLength' => 100],
            'password|密码' => ['required', 'minLength' => 6, 'maxLength' => 16],
            'password_confirm|密码' => ['equalAttribute' => 'password',],
            'nickname' => ['required', 'minLength' => 1, 'maxLength' => 20],
        ];

        $msg = [
            'email' => "请输入正确的邮箱地址",
            'password' => '请输入6-16位的密码',
            'password_confirm' => '两次输入密码不一致',
            'nickname' => '请输入用户名,1-20个字母或汉字'
        ];

        $validate = new Validator($rule, $msg);
        $validateResult = $validate->validate([
            'email' => $email,
            'password' => $password,
            'password_confirm' => $password_confirm,
            'nickname' => $nickname
        ]);
        if (!$validateResult) {
            return $this->json([
                'status' => -1,
                'msg' => '字段校验失败',
                'fail_field' => $validate->getErrors()
            ]);
        }

        //校验邮箱
        $userModel = new UserModel();
        $registeredUser = $userModel->getUserByEmail($email, 'userid,email,nickname');
        if (!empty($registeredUser)) {
            return $this->json([
                'status' => -2,
                'msg' => '该邮箱已被注册',
            ]);
        }

        //更新用户记录
        $userId = $userModel->addUser($email, $password, $nickname, UserModel::SOURCE_REGISTER);
        if ($userId > 0) {
            return $this->json([
                'status' => 0,
                'msg' => '注册成功',
            ]);
        }
        return $this->json([
            'status' => -99,
            'msg' => '系统繁忙,请稍后再试',
        ]);
    }

    public function login(Request $request){
        $email=$request->getPost("email");
        $password = $request->getPost('password');
        $userModel=new UserModel();
    }

    public function ldapLogin(Request $request){
        $email=$request->getPost("email");
        $password = $request->getPost('password');
    }

}